<template>
  <div></div>
</template>

<script>
import $utils from '../../utils'

export default {
  methods: {
    wxCodeSet() {
      const wxCodeAuth = () => {
        const wxLoginConfig = {
          appid: 'wxff7a18f70da090cd',
          redirect_uri: 'http://test.zechaoxinxi.com/baseService/public/getIndex/toUrl?type=' + location.origin + '/webPage/#/pages/eCNYpay1/index',
          response_type: 'code',
          scope: 'snsapi_base',
          // scope: 'snsapi_userinfo',
        }

        const onWxCode = () => {
          window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?${$utils.qsStringify(wxLoginConfig)}#wechat_redirect`
        }

        const getWxCode = () => {
          return new Promise((resolve, reject) => {
            const code = $utils.urlQuery('code')
            if (!code) {
              reject()
            } else {
              resolve(code)
            }
          })
        }

        getWxCode()
          .then((res) => {
            this.$api.bussinessApi
              .authorizedLogin(null, '/' + res)
              .then((res) => {
                localStorage.user = res.data.openId
              })
              .catch((res) => {
                uni.showModal({
                  title: '提示',
                  content: res || '授权出错！',
                  showCancel: false,
                  success: function(res) {
                    if (res.confirm) {
                      onWxCode()
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  },
                })
              })
          })
          .catch(() => {
            onWxCode()
          })
      }
      if (!localStorage.user) {
        wxCodeAuth()
      }
    },
    wxShare(res1) {
      let wx = window.wxSDK

      wx.config({
        // debug: true,
        appId: res1.appId,
        timestamp: res1.timestamp,
        nonceStr: res1.nonceStr,
        signature: res1.signature,
        jsApiList: [],
        // jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
      })

      wx.ready(() => {
        wx.hideOptionMenu()
        // let title = '夏日骄阳之金融知识有奖竞答',
        //   desc = '快来参与吧！',
        //   link = window.location.href,
        //   img = window.location.origin + '/answerweb/images/share.jpg'
        // wx.updateAppMessageShareData({
        //   title: title,
        //   desc: desc,
        //   link: link,
        //   imgUrl: img,
        //   success: () => {},
        // })
        // wx.updateTimelineShareData({
        //   title: title,
        //   link: link,
        //   imgUrl: img,
        //   success: () => {},
        // })
      })

      wx.error((response) => {
        console.log(response)
      })
    },
  },
}
</script>
